package com.zhihuichongcheng.application.ui.im.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.smartlib.cmnObject.util.SPUtil;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.bean.mine.UserBean;
import com.zhihuichongcheng.application.glide.GlideCircleTransform;
import com.zhihuichongcheng.application.ui.BaseApplication;
import com.zhihuichongcheng.application.ui.im.activity.AddMemberToGroupActivity;
import com.zhihuichongcheng.application.ui.im.activity.ChatActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 3water on 2016/10/31.
 */

public class GroupMemberAdapter extends BaseAdapter {

    private Context mContext;
    private SPUtil mSpUtil = null;
    private List<String> mListData = new ArrayList<>();
    private UserBean mUser;
    private String mGroupId;


    public GroupMemberAdapter(Context context)
    {
        this.mContext = context;
        mSpUtil = SPUtil.getInstance(context);
        BaseApplication app = (BaseApplication)context.getApplicationContext();
        mUser =app.getUser();
    }

    public void setGroupId(String groupId) {
        this.mGroupId = groupId;
    }

    @Override
    public int getCount() {
        return mListData.size();
    }

    @Override
    public Object getItem(int position) {
        return mListData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHold viewHold = null;
        if(convertView==null)
        {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.listadapter_group_member,null);
            viewHold = new ViewHold();
            viewHold.mImAvatar = (ImageView)convertView.findViewById(R.id.im_avatar);
            viewHold.mTvName = (TextView)convertView.findViewById(R.id.tv_name);
            convertView.setTag(viewHold);
        }
        else
        {
            viewHold = (ViewHold)convertView.getTag();
        }
        if(!TextUtils.isEmpty(mListData.get(position)))
        {
            UserBean bean = mSpUtil.getObject(mListData.get(position),UserBean.class);
            if(bean!=null)
            {
                viewHold.mTvName.setText(bean.getUserName());
                Glide.with(mContext).load(bean.getLogoUrl()).placeholder(R.drawable.ic_default_avatar).transform(new GlideCircleTransform(mContext)).into(viewHold.mImAvatar);
            }
            else
            {
                if(mUser.getEasemobNumber().equals(mListData.get(position)))
                {
                    viewHold.mTvName.setText(mUser.getUserName());
                    Glide.with(mContext).load(mUser.getLogoUrl()).placeholder(R.drawable.ic_default_avatar).transform(new GlideCircleTransform(mContext)).into(viewHold.mImAvatar);
                }
                else
                {

                }
            }
        }
        else {
            Glide.with(mContext).load(R.drawable.ic_chat_group_add_member).into(viewHold.mImAvatar);
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!TextUtils.isEmpty(mListData.get(position)))
                {
                    if(!mListData.get(position).equals(mUser.getEasemobNumber()))
                    {
                        Intent intent = new Intent(mContext, ChatActivity.class);
                        intent.putExtra("userId",mListData.get(position));
                        mContext.startActivity(intent);
                        ((Activity)mContext).overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
                    }
                }
                else
                {
                    Intent intent = new Intent(mContext, AddMemberToGroupActivity.class);
                    intent.putExtra("groupId",mGroupId);
                    mContext.startActivity(intent);
                    ((Activity)mContext).overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
                }

            }
        });
        return convertView;
    }

    public void setData(List<String> list) {
        this.mListData = list;
    }

    class ViewHold {
        private TextView mTvName;
        private ImageView mImAvatar;
        private View view;


    }
}
